﻿namespace Pp.Repository
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Web;
    using Core;
    using Core.Extentions;
    using Domain.Model;
    using Dto;
    using Globalization;
    using NHibernate;
    using NHibernate.Criterion;

    public class AngelRepository : BaseRepository<Angel>
    {
        private readonly ProjectRepository _projectRepository;
        public AngelRepository(ISession session, HttpServerUtilityBase server)
            : base(session, server)
        {
            _projectRepository = new ProjectRepository(session, server);
        }

        public Result Create(AngelCreate angelCreate)
        {
            var project = _projectRepository.Get(angelCreate.ProjectId);
            if (project == null) return Result.AsError(Res.Information_Making_project_selection);
            var isExist = IsExist(BaseCriteria.CreateAlias("Project", "p")
                .Add(Restrictions.Eq("p.Name", angelCreate.Name))
                        .Add(Restrictions.Eq("p.Id", project.Id)));
            if (isExist)
                return Result.AsError(Res.Information_There_is_aangel_for_this_project);
            var angel = new Angel
                            {
                                Description = angelCreate.Description,
                                Name = angelCreate.Name,
                                Project = project
                            };
            project.Angels.Add(angel);
            Save(angel);

            if (angelCreate.LogoFile != null)
            {
                var saveResult = SavePicture("/Content/Upload/Angel/", angelCreate.LogoFile, ImageSize.smallSize, project.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
                saveResult = SavePicture("/Content/Upload/Angel/", angelCreate.LogoFile, ImageSize.mediumSize, project.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
                saveResult = SavePicture("/Content/Upload/Angel/", angelCreate.LogoFile,ImageSize.largeSize, project.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
            }
            return Result.AsSuccess();
        }

        public Result<PageList<AngelDto>> GetAngelListById(int? page, Guid id)
        {
            var angelList = GetAll<AngelDto>(page, 10, "/Angel/Index/" + id, BaseCriteria.CreateAlias("Project", "p").Add(Restrictions.Eq("p.Id", id)));
            return Result<PageList<AngelDto>>.AsSuccess(angelList);
        }

        public AngelEdit GetEdit(Guid? id)
        {
            var angel = Get(id);
            return new AngelEdit
                       {
                           Description = angel.Description,
                           Id = angel.Id,
                           Name = angel.Name,
                           ProjectId = angel.Project.Id
                       };
        }

        public Result Edit(AngelEdit angelEdit)
        {
            var angel = Get(angelEdit.Id);
            angel.Name = angelEdit.Name;
            angel.Description = angelEdit.Description;
            Update(angel);

            if (angelEdit.LogoFile != null)
            {
                var saveResult = SavePicture("/Content/Upload/Angel/", angelEdit.LogoFile, ImageSize.smallSize, angel.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
                saveResult = SavePicture("/Content/Upload/Angel/", angelEdit.LogoFile, ImageSize.mediumSize, angel.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
                saveResult = SavePicture("/Content/Upload/Angel/", angelEdit.LogoFile, ImageSize.largeSize, angel.Id);
                if (!saveResult.Success) return Result.AsError(saveResult.Error);
            }
            return Result.AsSuccess();
        }

        public Result DeleteById(Guid? id)
        {
            var angel = Get(id);
            if (angel == null) return Result.AsError(Res.Information_Angel_NoFound_ForRemove);
            return Delete(angel);
        }
    }
}